<?php
class BaseStatic extends CApplicationComponent {
    public $all_categories = array();
    public $all_rubrics = array();
    public $rubrics = array();
    
    public $all_regions = array();
    public $all_cities = array();
    
    public function __construct() {
        $all_categories = Yii::app()->db->createCommand()
    		->select('c.*, r.name as rubric_name, r.description as rubric_description, r.service_type as rubric_service_type, r.quantity as rubric_quantity')
    		->from('categories c')
    		->leftJoin('rubrics r', 'r.id = c.rubric_id')
    		->where('c.hidden = 0 AND r.hidden = 0')
    		->queryAll();
        if (0 <= $all_categories) {
            $this->all_categories = $all_categories;
            $all_rubrics = array();
            foreach($all_categories as $cat) {
                if ($cat['rubric_service_type'] != $cat['service_type']) continue;
                $all_rubrics[$cat['rubric_id']]['id'] = $cat['rubric_id'];
                $all_rubrics[$cat['rubric_id']]['name'] = $cat['rubric_name'];
                $all_rubrics[$cat['rubric_id']]['description'] = $cat['rubric_description'];
                $all_rubrics[$cat['rubric_id']]['service_type'] = $cat['rubric_service_type'];
				$all_rubrics[$cat['rubric_id']]['quantity'] = $cat['rubric_quantity'];
                $all_rubrics[$cat['rubric_id']]['categories'][$cat['id']] = $cat;
            }
            $this->all_rubrics = $all_rubrics;
        }
        $rubrics = array();
        foreach ($this->all_rubrics as $r_id => $rubric) {
            $rubrics[$rubric['service_type']][$r_id] = $rubric;
        }
        $this->rubrics = $rubrics;
//        $this->regions = Regions::model()->with('cities')->visible()->findAll('t.country_id = :c_id', array(':c_id' => Yii::app()->params['default_country_id']));
        $all_cities = Yii::app()->db->createCommand()
    		->select('c.*, r.name as region_name, r.slug as region_slug, r.quantity as region_quantity')
    		->from('cities c')
    		->leftJoin('regions r', 'r.id = c.region_id')
    		->where('c.hidden = 0 AND r.hidden = 0 AND c.country_id = :c_id AND r.country_id = :c_id', array(':c_id' => Yii::app()->params['default_country_id']))
    		->queryAll();
        //$all_cities = Cities::model()->with(array('region'=>array('condition'=>'region.hidden=0')))->visible()->findAll('t.country_id = :c_id', array(':c_id' => Yii::app()->params['default_country_id']));
        if (0 < $all_cities) {
            $all_regions = array();
            $all_cities_tmp = array();
            foreach($all_cities as $one_city) {
                $all_cities_tmp[$one_city['id']] = $one_city;
                $all_regions[$one_city['region_id']]['id'] = $one_city['region_id'];
                $all_regions[$one_city['region_id']]['name'] = $one_city['region_name'];
                $all_regions[$one_city['region_id']]['slug'] = $one_city['region_slug'];
				$all_regions[$one_city['region_id']]['quantity'] = $one_city['region_quantity'];
                $all_regions[$one_city['region_id']]['cities'][$one_city['id']] = $one_city;
            }
            $this->all_regions = $all_regions;
            $this->all_cities = $all_cities_tmp;
        }
    }
    
    public function rubrics($service_type = 0) {
        return isset($this->rubrics[$service_type]) ? $this->rubrics[$service_type] : array();
    }
    
    public function categories($rubric_id = 0) {
        return (isset($this->all_rubrics[$rubric_id]['categories'])) ? $this->all_rubrics[$rubric_id]['categories'] : array();
    }
    
    public function region($region_id) {
        return isset($this->all_regions[$region_id]) ? $this->all_regions[$region_id] : array();
    }
/*    
    public function city($region_id, $city_id) {
        $region = $this->region($region_id);
        if (!isset($region['cities'])) return array();
        if (!isset($region['cities'][$city_id])) return array();
        return $region['cities'][$city_id];
    }
*/
    public function city($city_id) {
        return isset($this->all_cities[$city_id]) ? $this->all_cities[$city_id] : array();
    }
    
}
?>

